#pragma once

#include <stdint.h>

#define USB_TIMEOUT (60000)

/******************************USB PROTOCOL DEFINE*****************************/
#define UVC_PROTOCOL 0
#define HID_PROTOCOL 1

#define SPL_HEADER_LOAD_ADDR          (0x002f0000 + 0x40)
#define SPL_HEADER_MAX_SIZE           (512)
#define SPL_IMAGE_SIZE_ALIGN          (512)
#define ARTO_USB_EP0_MAX_PAYLOAD_SIZE (ARTO_USB_EP0_TRAN_SIZE - sizeof(struct artousb_cmd_header))
#define ARTO_USB_EXIT_LEN             0x0
#define ARTO_USB_EXIT_ADDR            0x0

#define ARTO_UPGRADE_MAGIC    (0x8030)
#define ARTO_UPGRADE_MAX_SIZE (0x1000)

#define ARTO_HID_MAGIC_0   'A'
#define ARTO_HID_MAGIC_1   'U'
#define ARTO_HID_PORT_SYS  (0x0)
#define ARTO_HID_PORT_ECHO (0x1)
#define ARTO_HID_PORT_RECV (0x2)
#define ARTO_HID_PORT_SEND (0x3)
#define ARTO_HID_PORT_UPGD (0x4)
#define ARTO_HID_MAX_SIZE  (0x2000)

#define 	OTA_IMG_MAGIC 			(0x4152544f)

#define     GPT_FLASH_OFFSET        (0x8000)
#define     GPT_FLASH_SIZE          (0x1000)

#pragma pack(push)
#pragma pack(1)
typedef struct {
    uint8_t  magic[2];
    uint16_t port_index;
    uint16_t header_len;
    uint32_t data_len;
    uint8_t  status;
    uint8_t  checksum;
} STRU_ARTO_HID_HEADER;

typedef struct {
    uint16_t magic;
    uint8_t  cmd;
    uint8_t  response;
    uint16_t length;
    uint16_t reserve;
    uint32_t addr_l;
    uint32_t addr_h;
} STRU_ARTO_UPGRADE_HEADER;

typedef struct {
    uint32_t length;
    uint32_t target_crc;
} STRU_ARTO_UPGRADE_CRC_HEADER;

struct upgrade_hdr {
    uint32_t magic;
    uint8_t  hdr_version;
    uint8_t  compressed;
    uint8_t  flashtype;   /* 0:spinor 1:emmc 2:spinand */
    uint8_t  part_status; /* whether partitions are changed */
    uint16_t header_ext_size;
    uint16_t hash_size;
    uint16_t sig_size;
    uint16_t sig_realsize;
    /* size of the image which lays after signature */
    uint64_t img_size;
    uint32_t rom_size;
    uint32_t loader_size;
    uint16_t partitions;
    uint16_t segments;
    uint32_t object_version; // object_version > depend_version
    uint32_t depend_version;
    uint8_t  reserve[20];
    uint8_t  part_flag[64];    /* 64 */
    uint8_t  sdk_version[128]; /* 128 */
};

/* Upgrade parition table entry */
struct part_info {
    uint8_t  name[32];
    uint64_t flash_offset;
    uint64_t length;

    uint32_t is_upgrade;
};

/* Upgrade image segment table */
struct segment_info {
    uint64_t img_offset;
    uint64_t flash_offset;
    uint64_t size_compress;
    uint64_t size_decompress;
};

#pragma pack(pop)